package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Contact;
import com.kaishengit.entity.Goal;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;

public class ContactDao {
	private DBHelp<Contact> db = new DBHelp<Contact>();

	public void save(Contact contact) {
		String sql = "insert into t_contact (id,`name`,companyname,mobile,tel,email,address,website,weibo,content,projectid) values (?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, contact.getId(),contact.getName(),contact.getCompanyname(),contact.getMobile(),contact.getTel(),contact.getEmail(),contact.getAddress(),contact.getWebsite(),contact.getWeibo(),contact.getContent(),contact.getProjectid());
	}

	public List<Contact> findByProjectid(String projectid) {
		String sql = "select * from t_contact where projectid =? order by createtime desc";
		return db.executeQueryToList(sql, new ContactRowMapper(), projectid);
	}

	public class ContactRowMapper implements RowMapper<Contact>{

		public Contact mapRow(ResultSet rs) throws SQLException {
			Contact contact = new Contact();
			contact.setId(rs.getString("id"));
			contact.setName(rs.getString("name"));
			contact.setCompanyname(rs.getString("companyname"));
			contact.setMobile(rs.getString("mobile"));
			contact.setTel(rs.getString("tel"));
			contact.setEmail(rs.getString("email"));
			contact.setAddress(rs.getString("address"));
			contact.setWebsite(rs.getString("website"));
			contact.setWeibo(rs.getString("weibo"));
			contact.setCreatetime(rs.getString("createtime"));
			contact.setContent(rs.getString("content"));
			contact.setProjectid(rs.getString("projectid"));
			return contact;
		}
		
	}

	public Contact findById(String cid) {
		String sql = "select * from t_contact where id = ?";
		return db.executeQueryToObject(sql, new ContactRowMapper(), cid);
	}

	public void edit(String id, String name, String companyname, String mobile,
			String tel, String email, String address, String website,
			String weibo, String content) {
		String sql = "update t_contact set name = ?,companyname = ?, mobile=?,tel=?,email=?,address=?,website=?,weibo=?,content=? where id=?";
		db.executeSQL(sql, name,companyname,mobile,tel,email,address,website,weibo,content,id);
		
	}

	public void del(String id) {
		String sql = "delete from t_contact where id = ?";
		db.executeSQL(sql, id);
		
	}

	
}
